const { Validator, Rule } = require('../../../../../core/validator/index')

const { LOGIN_TYPE } = require('../../../../lib/enum')

class LoginValidator extends Validator {
  constructor() {
    super()
    this.account = [
      new Rule('isLength', '不符合长度规则', {
        min: 4,
        max: 32,
      }),
    ]
    this.secret = [
      new Rule('isOptional'),
      new Rule('isLength', '不符合长度规则', {
        min: 4,
        max: 32,
      }),
    ]
  }

  validateLoginType(requestParams) {
    const { type } = requestParams.body
    if (!type) {
      throw new Error('登录类型 type 是必须字段')
    }
    if (!LOGIN_TYPE.isThisType(type)) {
      throw new Error('请传入正确的登录类型 type')
    }
  }
}

const verify = async (ctx, next) => {
  await new LoginValidator().start(ctx)
  await next()
}

module.exports = {
  verify,
}
